﻿using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using Grinder.Renamer;
using System;

namespace Grinder.Renamer.RenamePlugins
{
    internal sealed class CoreRenamePlugin : IRenamePlugin
    {
        private readonly Dictionary<string, string> _tokens;

        public IEnumerable<string> Extensions { get { return new List<string> { "*.*" }; } }
        public string Name { get { return "All Files"; } }
        public Dictionary<string, string> Tokens { get { return _tokens; } }
        public Image Icon { get { return Properties.Resources.asterisk_orange; } }
        public override string ToString() { return string.Format("{0} ({1})", Name, string.Join(",", Extensions.ToArray()).ToLower()); }

        public CoreRenamePlugin()
        {
            _tokens = new Dictionary<string, string>
                         {
                             {"#",        "Counter"},
                             {"$",        "Original file name"},
                             {"&",        "Original file name (UPPER CASE)"},
                             {"%",        "Original file name (lower case)"},
                             {"*",        "Original file name (Capitalized)"},
                             {"\\",         "New sub-folder"},
                             {"parent",   "Parent folder name"},
                             {"size",     "File size"},                             
                             {"date",     "Current date"},
                             {"mdate",    "Modification date"},
                             {"cdate",    "Creation date"},
                             {"length",   "File name length"},
                             {"substr",   "Substring. E.g. [substr:0,10]"}                             
                         };
        }

        public Dictionary<string, object> GetTokens(FileInfo file)
        {
            var dic = new Dictionary<string, object>();
            var fileName = file.GetNameWithoutExtension();

            dic.Add("$", fileName);
            dic.Add("&", fileName.ToUpper());
            dic.Add("%", fileName.ToLower());
            dic.Add("*", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fileName));
            dic.Add("parent", (file.Directory == null ? "" : file.Directory.Name));
            dic.Add("size", file.Length);
            dic.Add("date", DateTime.Now);
            dic.Add("mdate", file.LastWriteTime);
            dic.Add("cdate", file.CreationTime);
            dic.Add("length", fileName.Length);
            
            return dic;
        }
    }
}
